package controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import dao.LogDao;
import dao.UsersDao;
import util.MybatisSessionFactory;

@Controller
public class controller {
     @RequestMapping(value="/register.action",method=RequestMethod.POST)
     @ResponseBody
     public String register(
    		 @RequestParam(value="account",defaultValue="")String account,
		     @RequestParam(value="password",defaultValue="")String password,
		     @RequestParam(value="password2",defaultValue="")String password2 ){
    	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	     UsersDao dao=Session.getMapper(UsersDao.class);
	     LogDao dao1=Session.getMapper(LogDao.class);
         Map<String,Object> result=dao.query(account);
         if("1".equals(result.get("counter").toString())){return "ERR";}
         dao.add(account,password,"2078-09-09");
         Map<String,Object> map=dao.queryid();
	     dao1.add1(map.get("id").toString(),"0", "注册");
         Session.commit();
         return "OK";
         }		 
 
 @RequestMapping(value="/add1.action",method=RequestMethod.POST)
 @ResponseBody
 public String add1(
		 @CookieValue(value="user",defaultValue="")String userId,
		  HttpServletResponse response,
		 @RequestParam(value="date_time",defaultValue="")String date_time,
		 @RequestParam(value="money_in",defaultValue="")String money_in,
		 @RequestParam(value="place",defaultValue="")String place,
		 @RequestParam(value="kind",defaultValue="")String kind,
		 @RequestParam(value="remark",defaultValue="")String remark){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 LogDao dao1=Session.getMapper(LogDao.class);
	 dao.add1(userId,date_time,money_in,place,kind,remark);
	 Map<String,Object> map=dao.queryid();
	 dao1.add1(userId,map.get("id").toString(),"收入");
	 Session.commit();
	 return "OK";
 }
 
 @RequestMapping(value="/add2.action",method=RequestMethod.POST)
 @ResponseBody
 public String add2(
		 @CookieValue(value="user",defaultValue="")String userId,
		  HttpServletResponse response,
		 @RequestParam(value="date_time",defaultValue="")String date_time,
		 @RequestParam(value="money_out",defaultValue="")String money_out,
		 @RequestParam(value="place",defaultValue="")String place,
		 @RequestParam(value="kind",defaultValue="")String kind,
		 @RequestParam(value="remark",defaultValue="")String remark){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 LogDao dao1=Session.getMapper(LogDao.class);
	 dao.add2(userId, date_time, money_out, place, kind, remark);
	 Map<String,Object> map=dao.queryid();
	 dao1.add1(userId,map.get("id").toString(),"支出");
	 Session.commit();
	 return "OK";
 }
 
 @RequestMapping(value="/login.action",method=RequestMethod.POST)
 @ResponseBody
 public String login(
		 @RequestParam(value="account",defaultValue="")String account,
		 @RequestParam(value="password",defaultValue="")String password,
         HttpServletResponse response
		 ){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
	 LogDao dao1=Session.getMapper(LogDao.class);
     Map<String,Object> user=dao.queryone(account,password);
     if("0".equals(user.get("counter").toString())){return "ERR";}
     response.addCookie(new Cookie("user",user.get("id").toString()));
     dao1.add1(user.get("id").toString(),"0", "登录");
     Session.commit();
     return "OK";
 }
 
 @RequestMapping(value="/querynick.action",method=RequestMethod.GET)
 @ResponseBody
 public Map<String, Object> querynick(
		 @CookieValue(value="user",defaultValue="")String userId,
		  HttpServletResponse response
		 ){
	 if("".equals(userId)){
		 try {
			response.sendRedirect("login.html");
		} catch (IOException e) {
			System.out.println("[error] getUserInfo.action:redirect\r\n");	
		}
		 return null;
	 }
	 SqlSession session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=session.getMapper(UsersDao.class);
     Map<String,Object> result=dao.querynick(userId);
     session.close();
     return result;
	}
 
 @RequestMapping(value="/queryuser.action",method=RequestMethod.GET)
 @ResponseBody
 public Map<String,Object> queryuser(
		 @RequestParam(value="user_id",defaultValue="")String user_id
		 ){
		SqlSession session=MybatisSessionFactory.getSqlSession();
		UsersDao d=session.getMapper(UsersDao.class);
		Map<String,Object> list=d.queryuser("1");
		return list;
	}
 
 @RequestMapping(value="/queryday.action",method=RequestMethod.GET)
 @ResponseBody
 public List<Map> queryday(
		 @RequestParam(value="date_time",defaultValue="")String date_time
		 ){
		SqlSession session=MybatisSessionFactory.getSqlSession();
		UsersDao d=session.getMapper(UsersDao.class);
		List<Map> list=(List<Map>) d.queryday(date_time);
		    return list;
	}
 
 @RequestMapping(value="/query.action",method=RequestMethod.POST)
 @ResponseBody
 public List<Map> query(
		 @CookieValue(value="user",defaultValue="")String userId,
		  HttpServletResponse response,
		 @RequestParam(value="user_id",defaultValue="")String user_id,
		 @RequestParam(value="date_time",defaultValue="")String date_time,
		 @RequestParam(value="place",defaultValue="")String place,
		 @RequestParam(value="money_in",defaultValue="")String money_in,
		 @RequestParam(value="money_out",defaultValue="")String money_out,
		 @RequestParam(value="kind",defaultValue="")String kind,
		 @RequestParam(value="remark",defaultValue="")String remark
		 ){
	 SqlSession Session=MybatisSessionFactory.getSqlSession();
	 UsersDao dao=Session.getMapper(UsersDao.class);
//	 LogDao dao1=Session.getMapper(LogDao.class);
//	 System.out.println(date_time+place+money_in+money_out+kind+remark);
	 List<Map> map=dao.queryq(userId, date_time, place, money_in, money_out, kind, remark);
//     response.addCookie(new Cookie("user",map.get("id").toString()));
//     dao1.add1(map.get("id").toString(),"0", "查询");
     Session.commit();
	 return map;
 }
 
 @RequestMapping(value="/querys.action",method=RequestMethod.GET)
 @ResponseBody
 public List<Map> querysday(
		 @CookieValue(value="user",defaultValue="")String userId,
		  HttpServletResponse response		 ){
		SqlSession session=MybatisSessionFactory.getSqlSession();
		UsersDao d=session.getMapper(UsersDao.class);
		List<Map> list=d.querys(userId);
		    return list;
	}
 
 @RequestMapping(value="/quit.action",method=RequestMethod.POST)
 @ResponseBody
 public String quit(
		 @CookieValue(value="user",defaultValue="")String userId,
		  HttpServletResponse response
		 ){
	   SqlSession session=MybatisSessionFactory.getSqlSession();
	   response.addCookie(new Cookie("user",null));
	   return "OK";
 }
 
 @RequestMapping(value="/queryd.action",method=RequestMethod.GET)
 @ResponseBody
 public List<Map> queryd(
		 @CookieValue(value="user",defaultValue="")String userId,
		 @RequestParam(value="id",defaultValue="")String id,
		 HttpServletResponse response
		 ){
	    SqlSession session=MybatisSessionFactory.getSqlSession();
		UsersDao d=session.getMapper(UsersDao.class);
		List<Map> list=d.queryd(id, userId);
		return list;
 }
 
 @RequestMapping(value="/del.action",method=RequestMethod.POST)
 @ResponseBody
 public String del(
		 @CookieValue(value="user",defaultValue="")String userId,
		 @RequestParam(value="id",defaultValue="")String id,
		 HttpServletResponse response
		 ){
	    System.out.println(id);
	    SqlSession session=MybatisSessionFactory.getSqlSession();
		UsersDao d=session.getMapper(UsersDao.class);
		LogDao dao1=session.getMapper(LogDao.class);
		d.del(id,userId);
		dao1.add1(userId,id,"删除");
		session.commit();
		return "OK";
 }
// public static void main(String[] args) {
//	    SqlSession session=MybatisSessionFactory.getSqlSession();
//		UsersDao d=session.getMapper(UsersDao.class);
//		Map<String,Object> map=d.queryday();
//		System.out.println(list);
//}
}
